(function($){
	
	
	$.fn.sticky = function(options) {
		var settings = $.extend({}, $.fn.sticky.defaults, options);

		
		return this.each(function(){
			var obj = $(this);
			obj.css({position: "absolute", zIndex:settings.zIndex, height: settings.height, width: settings.width});
			
			moveObject(obj, settings, false);
					
			$(window).scroll(function(){
				if(obj.is(":visible"))
					moveObject(obj, settings, true);
			});
		});
	};
	
	

	function moveObject (obj, settings, animate){
		var scrollTopPosition = $(window).scrollTop();
		var scrollLeftPosition = $(window).scrollLeft();
		
		//FIX in version 1.3
		var heightWindow = document.getElementsByTagName('html')[0].clientHeight;
		var widthWindow = document.getElementsByTagName('html')[0].clientWidth;
		
		//var newPosition = {top: 0, left: 0};
		var newPosition = settings.dest.position();
		
		newPosition.top+=settings.dest.height();

		var newProps = {top: newPosition.top+"px", left:newPosition.left+"px"};
		
		if(!animate){
			obj.css(newProps);
		}else{
			obj.stop({clearQueue: true, gotoEnd:true});
			obj.animate(newProps, settings.duration);
		}
	};
	
	$.fn.sticky.defaults  = {
			duration: 50,
			margin: 10,
			dest: $(document),
			zIndex: 2,
			height: "100px",
			width: "100px"
	};
	
	
	
	
})(jQuery);
